package com.example.ticket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ticket.domain.PageDomain;
import com.example.ticket.entity.User;
import com.example.ticket.mapper.UserMapper;
import com.example.ticket.service.MailService;
import com.example.ticket.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserServiceimpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailService mailService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageInfo<User> page(PageDomain pageDomain){
        PageHelper.startPage(pageDomain.getPageNum(),
                pageDomain.getPageSize(),pageDomain.getOrderStr());
        return new PageInfo<>(userMapper.Userlist());
    }

    @Override
    public List<User> Userlist(){
/*        Object object=redisTemplate.opsForValue().get("user");
        if(object!=null){
            return (List<User>)object;
        }else{*/
        List<User> list = userMapper.Userlist();
/*        redisTemplate.opsForValue().set("user",list,1,TimeUnit.HOURS);*/
        return list;
/*        }*/
    }

    @Override
    public User checkEmail(String email){
        return userMapper.checkEmail(email);
    }

    @Override
    public User selectById(Integer id){
        Object object=redisTemplate.opsForValue().get("user_"+id);
        if(object!=null){
            return (User)object;
        }else{
            User user= userMapper.selectById(id);
            redisTemplate.opsForValue().set("user_"+id,user,20, TimeUnit.MINUTES);
            return user;
        }
    }

    @Override
    public int ResetPassowrd(String password,String username){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(password);
        int res=userMapper.ResetPassowrd(encode,username);
        return res;
    }

    @Override
    public User selectByUsername(String username){
        Object object=redisTemplate.opsForValue().get("user_"+username);
        if(object!=null){
            return (User)object;
        }else{
            User user= userMapper.selectByUsername(username);
            redisTemplate.opsForValue().set("user_"+username,user,20, TimeUnit.MINUTES);
            return user;
        }
    }

    @Override
    public  List<User> findByUsername(String username){
        return userMapper.findByUsername(username);
    }

    @Override
    public int deleteById(Integer id){
        int res=userMapper.deleteById(id);
        if(res==1){
        redisTemplate.delete("user_"+id);
        }
        return res;
    }

    @Override
    public User freezeById(User user){
        int res=userMapper.freezeById(user.getStatus(),user.getId());
        if(res==1){
            redisTemplate.opsForValue().set("user_"+user.getId(),user,20, TimeUnit.MINUTES);
            System.out.println("冻结or激活成功");
        }
        return user;
    }

    @Override
    public User updateUser(User user){
/*        //Bcrypt算法
        int t=0;
        for (t = 1; t <= 10; t++) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String hashedPassword = passwordEncoder.encode(user.getPassword());
            user.setPassword(hashedPassword);
        }*/


/*        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(user.getPassword());
        user.setPassword(encode);*/
        int res=userMapper.updateUser(user);
        if(res==1){
            redisTemplate.opsForValue().set("user_"+user.getId(),user,20, TimeUnit.MINUTES);
            System.out.println("用户数据更新成功");
        }
        return user;
    }

    @Override
    public User insertUser(User user){

        //获取注册信息
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(user.getPassword());
        user.setPassword(encode);
        if(userMapper.insertUser(user)!=0){
        //进行邮箱激活
        String code=user.getCode();
        System.out.println("code:"+code);
        //主题
        String subject = "来自408票据管理系统的激活邮件";
        //user/checkCode?code=code(激活码)是我们点击邮件链接之后根据激活码查询用户，如果存在说明一致，将用户状态修改为“1”激活
        //上面的激活码发送到用户注册邮箱
        String context = "<div><h1>欢迎使用408票据管理系统</h1></div>" +
                "<div>点击下方激活您的账号</div>" +
                "<a href=\"http://120.77.150.238:8089/checkCode?code="+code+"\">点击激活您的账号"+"</a>" +
                "<div>若有疑问，请联系客服-电话12345678</div>";
        //发送激活邮件
        mailService.sendHtmlMail (user.getEmail(),subject,context);
        //redisTemplate.opsForValue().set("user_"+user.getUsername(),user);
        }
        return user;
    }

    @Override
    public User checkCode(String code){
        return userMapper.checkCode(code);
    }

    @Override
    public int insertUserAuthority(Integer id){
        return userMapper.insertUserAuthority(id);
    }
}
